<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Canvas extends PC_controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Projectmodel','m_project',true);
	}

	public function index($project_id){
		$project = $this->m_project->getProject($project_id);
		if(empty($project)) show_404();

		$list = $this->m_project->getProjectSpriteList($project_id);

		foreach ($list as $key => $record) {
			$list[$key]['sprite_url'] = UPLOAD_URL.$record['sprite_path'];
			$record['sprite_path'] = UPLOAD_PATH.$record['sprite_path'];
			$size = @getimagesize($record['sprite_path']);
			$list[$key]['width'] = $size[0];
			$list[$key]['height'] = $size[1];


			if(!file_exists($record['sprite_path'])) {
				unset($list[$key]);
				continue;
			}
		}
		$list = array_values($list);

		$data = parent::index();
		$data['attr'] = array(
			'project_id' => $project_id,
			'project_name' => $project['project_name'],
			'project_code' => $project['project_code'],
			'json' => json_encode($list),
			'width' => $project['project_sprite_width'],
			'height' => $project['project_sprite_height'],

		);
		$this->load->view('canvas',$data);
	}


	public function addImage($project_id = 0){
		if(!$this->m_app->initFolder()){
			$this->response->setDetail($this->lang->line('error_update'));
			$this->response->responseExit();
		}

		$project = $this->m_project->getProject($project_id);
		if(empty($project)) {
			$this->response->setDetail('项目错误');
			$this->response->responseExit();
		}
		$old = umask(0);
		@mkdir(UPLOAD_PATH.$project['project_code'].'/sprite/',0777,true);
		umask($old);

		$existed_sprite = $this->m_project->getProjectSpriteList($project_id);
		$existed_sprite = reindexArray($existed_sprite,'sprite_name');

		$filename = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : 'a.png');
		$path = $project['project_code'].'/sprite/'.$filename;
		$file_pos = UPLOAD_PATH.$path;
		file_put_contents($file_pos,file_get_contents('php://input'));

		$file_info = array();

		$existed = isset($existed_sprite[$filename]);
		if(!$existed) {
			$file_info = array(
				'project_id' => $project_id,
				'sprite_name' => $filename,
				'sprite_path' => $project['project_code'].'/sprite/'.$filename,
				'sprite_x' => 0,
				'sprite_y' => 0,
				'sprite_status' => STATUS_ACTIVE,
				'sprite_time_create' => NOW,
			);
		} else {
			$sprite_id = $existed_sprite[$filename]['sprite_id'];
		}

		if(!empty($file_info)) {
			$sprite_id = $this->m_project->createProjectSprite($file_info);
		}
		$wh = getimagesize($file_pos);
		
		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->setAddon(array('sprite_id' => $sprite_id
			,'sprite_url'=>UPLOAD_URL.$path
			,'existed' => $existed
			,'sprite_name' => $filename
			,'width' => $wh[0]
			,'height' => $wh[1]
		));
		$this->response->responseExit();
	}

	public function getProjectSpriteList(){
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$project = $this->m_project->getProject($info['project_id']);
		$list = $this->m_project->getProjectSpriteList($info['project_id']);

		foreach ($list as $key => &$record) {
			$record['sprite_url'] = UPLOAD_URL.$record['sprite_path'];
			$record['sprite_path'] = UPLOAD_PATH.$record['sprite_path'];
			if(!file_exists($record['sprite_path'])) {
				unset($list[$key]);
				continue;
			}
			list($width, $height, $type, $attr)= getimagesize($record['sprite_path']);
			$record['width'] = $width;
			$record['height'] = $height;

			$record['sprite_path'] = trim($record['sprite_path'],'.');
		}
		$list = array_values($list);
		$count = count($list);

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->setAddon(array(
			'width' => $project['project_sprite_width'],
			'height' => $project['project_sprite_height'],
		));
		$this->response->responseExit();
	}

	public function saveSprite(){
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('width','integer');
		$this->validate->setRule('width','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('height','integer');
		$this->validate->setRule('height','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('sprite','array');
		$this->validate->setRule('sprite','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		if(empty($info['sprite'])){
			
			$this->response->setDetail($this->lang->line('error_parameter'));
			$this->response->responseExit();
		}

		$project = $this->m_project->getProject($info['project_id']);
		if(empty($project)){
			
			$this->response->setDetail($this->lang->line('error_parameter'));
			$this->response->responseExit();
		}

		foreach($info['sprite'] as &$sprite){
			$sprite['sprite_id'] = intval($sprite['sprite_id']);
			$sprite['sprite_x'] = intval($sprite['sprite_x']);
			$sprite['sprite_y'] = intval($sprite['sprite_y']);
			$sprite['sprite_status'] = intval($sprite['sprite_status']);
		}

		$this->m_project->editProjectSpriteBatch($info['sprite']);
		$this->m_project->editProject($info['project_id'],array(
			'project_sprite_width' => $info['width'],
			'project_sprite_height' => $info['height'],
		));
		$this->m_project->generateSprite($project,$info['width'],$info['height']);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function deleteSprite(){
		$this->validate->setRule('sprite_id','integer');
		$this->validate->setRule('sprite_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();

		$sprite[] = array(
			'sprite_id' => $info['sprite_id'],
			'sprite_status' => STATUS_DELETE,
		);
		$this->m_project->editProjectSpriteBatch($sprite);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}
}
/* End of file canvas.php */
/* Location: ./application/controllers/canvas.php */
